const { verifyToken } = require('../utils/jwt')
const logger = require("../utils/log4js");
const { wsSend } = require('../utils/utils')

module.exports = (ws, token, online) => {

    try {
        let userInfo = verifyToken(token)
        let index = online.findIndex(item => item.userInfo._id === userInfo.data._id)
        if (index > -1) {
            // 用户已在线
            online.splice(index, 1)

        }
        online.push({ userInfo: userInfo.data, ws })
        logger.info('更新在线用户：：', online.map(it => it.userInfo.name))
        wsSend(ws, '/login', {
            code: 1,
            msg: '登录成功!'
        })
    } catch (e) {
        logger.info('catch', e)
        wsSend(ws, '/login', {
            code: 1,
            msg: e
        })
    }
}